package com.nan.samsung.qrsan;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class SettingsActivity extends Activity {

	private EditText mIpEt;
	private EditText mFuncEt;
	private SharedPreferences mSp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_settings);

		mSp = getSharedPreferences(ZXingConstants.MY_SP, Context.MODE_PRIVATE);
		mIpEt = (EditText) findViewById(R.id.et_ip);
		mFuncEt = (EditText) findViewById(R.id.et_func);

		mIpEt.setText(mSp.getString(ZXingConstants.MY_IP, ZXingConstants.DEFAULT_IP));
		mFuncEt.setText(mSp.getString(ZXingConstants.MY_FUNC, ZXingConstants.DEFAULT_FUNC));

		super.onCreate(savedInstanceState);
	}

	public void onBack(View view) {
		finish();
	}

	public void onSave(View view) {
		String ip = mIpEt.getText().toString();
		String func = mFuncEt.getText().toString();
		if (ip.trim().isEmpty() || func.trim().isEmpty()) {
			Toast.makeText(this, "Please input legal IP address and function name", Toast.LENGTH_LONG).show();
		} else {
			mSp.edit().putString(ZXingConstants.MY_IP, ip).putString(ZXingConstants.MY_FUNC, func).commit();
			Toast.makeText(this, "Save successfully", Toast.LENGTH_LONG).show();
		}
		finish();
	}
}
